package cn.xenosp.server.common.tools;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * 项目名： service-web
 * 包路径： cn.xenosp.server.common.tools
 * 作者：   TongHui
 * 创建时间: 2024-08-25 14:13
 * 描述: 加载 properties 配置文件
 * 版本: 1.0
 */
@Slf4j
public class PropertiesUtil {
   private static Map<Object, Object> properties;

   public static void instance(List<String> propertiesNameList){
       if (properties == null){
           InputStream in = null;
           try {
               properties = new HashMap<>();
               for (String name : propertiesNameList) {
                   log.info("初始化【properties】配置文件：{}",name);
                   in = PropertiesUtil.class.getResourceAsStream("/"+name);
                   Properties prop = new Properties();
                   if (in != null) {
                       prop.load(in);
                   }
                   properties.putAll(prop);
               }
           } catch (IOException e) {
               e.printStackTrace();
           }finally {
               try {
                   if (in != null){
                       in.close();
                   }
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
       }

   }

   public static String get(String name){
       return properties.get(name).toString();
   }
}
